<template>
  <div class="RightFzjkDetails bigTableStyle">
    <div class="elForm-box">
      <el-form ref="elForm" :model="formData" size="medium" label-width="1.2rem">
        <el-row :gutter="15">
          <el-col :span="6">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="变电站名称:" prop="time">
                  <el-select
                    v-model="formData.station"
                    placeholder="请选择"
                    clearable
                    disabled
                    :style="{ width: '100%' }"
                    filterable
                  >
                    <el-option
                      v-for="(item, index) in stationList"
                      :key="index"
                      :label="item.stationName"
                      :value="item.stationId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="6">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="所属系统:" prop>
                  <el-select
                    v-model="formData.system"
                    placeholder="请选择"
                    clearable
                    filterable
                    :style="{ width: '100%' }"
                  >
                    <el-option
                      v-for="(item, index) in getSystemList"
                      :key="index"
                      :label="item.systemName"
                      :value="item.systemId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="6">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="所属区域:" prop="area">
                  <el-select
                    v-model="formData.area"
                    placeholder="请选择"
                    clearable
                    disabled
                    :style="{ width: '100%' }"
                  >
                    <el-option
                      v-for="(item, index) in getAreaList"
                      :key="index"
                      :label="item.areaName"
                      :value="item.areaId"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="6">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="所属设备:" prop="source">
                  <el-input
                    :style="{ width: '100%' }"
                    v-model="formData.deviceName"
                    placeholder="请输入"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-row :gutter="15">
          <el-col :span="6">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="测点名称:" prop="time">
                  <el-input
                    :style="{ width: '100%' }"
                    v-model="formData.pointName"
                    placeholder="请输入"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="6">
            <el-row :gutter="15">
              <el-col :span="24">
                <el-form-item label="测点类型:" prop>
                  <el-select
                    v-model="formData.pointType"
                    placeholder="请选择"
                    clearable
                    filterable
                    :style="{ width: '100%' }"
                  >
                    <el-option
                      v-for="(item, index) in getPointTypeList"
                      :key="index"
                      :label="item.pointTypeName"
                      :value="item.pointId"
                      :disabled="item.disabled"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
          <el-col :span="12">
            <div class="inspection-report-index-btn">
              <el-button type="primary" @click="init">查询</el-button>
              <el-button type="primary" @click="submitForm">新建</el-button>
              <el-button type="primary" @click="cutOut">删除</el-button>
              <el-button type="primary" @click="modify">修改</el-button>
              <el-button type="primary" @click="templateDownload">模板下载</el-button>
              <el-button type="primary" @click="mbgx">模板导入</el-button>
            </div>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="tableed bigTableStyle">
      <el-table
        border
        :data="tableData"
        height="100%"
        :row-class-name="tableRowClassName"
        @selection-change="handleSelectionChange"
        style="width: 100%;"
      >
        <el-table-column type="selection" width="50"></el-table-column>
        <el-table-column prop="myIndex" label="序号" width="60"></el-table-column>
        <el-table-column prop="stationNAME" label="变电站名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="sysname" label="所属系统名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="areaName" label="所属区域名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="deviceName" label="所属辅控设备名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="pointName" label="测点名称" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="lxMc" label="测点类型" :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="remoteType" label="四遥类型" :show-overflow-tooltip="true"></el-table-column>
      </el-table>
    </div>
    <div class="paged elPaginationStyle">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pages.page"
        :page-sizes="[10, 15, 20]"
        :page-size="pages.pageSize"
        layout="total,prev, pager, next,sizes, jumper"
        :total="pages.totalSize"
      ></el-pagination>
    </div>
    <el-dialog
      :title="PopUptitle"
      :visible.sync="dialogVisible"
      width="8.49rem"
      :before-close="handleClose"
    >
      <RightCdwhDetailsAdd
        @initList="init()"
        :bdzIds="bdzId"
        :multipleSelection="multipleSelection"
        :isShow="isShow"
        :PopUptitle="PopUptitle"
        @determine="determine"
        v-if="dialogVisible"
      />
    </el-dialog>
    <input
      id="file0"
      type="file"
      name="file"
      v-if="isShowFile"
      accept="application/vnd.ms-excel"
      @change="fileUpload"
    />
  </div>
</template>

<script lang="ts">
import { Component, Vue, Watch } from "vue-property-decorator";
import time from "@/utils/time.js";
import RightCdwhDetailsAdd from "./RightCdwhDetailsPopUp/RightCdwhDetailsAdd.vue";
import { ParameterManagementModule } from "@/store/modules/ParameterManagement";
@Component({
  components: { RightCdwhDetailsAdd },
})
export default class RightFzjkDetails extends Vue {
  //弹框状态
  public dialogVisible: boolean = false;

  //弹框title
  public PopUptitle: string = "";
  //搜索框from表单数据
  formData = {
    station: "", //变电站
    system: "", //所属系统
    area: "", //所属区域
    deviceName: "", //所属设备
    pointName: "", //测点名称
    pointType: "", //测点类型
  };
  citiesOptions = [
    {
      interval: "山东省国网公司",
      id: 1,
    },
  ];
  substationOptions = [
    {
      interval: "110kVxxx地变电站",
      id: 1,
    },
  ];
  sideShotOptions = [
    {
      interval: "xxx测点",
      id: 1,
    },
  ];
  systemOptions = [
    {
      interval: "xxxxxx系统",
      id: 1,
    },
  ];
  refreshOptions = [
    {
      interval: "其它类型",
      id: 1,
    },
  ];
  stationList = [];
  //选择数据
  public multipleSelection: Array<any> = [];
  //table表格相关事件数据
  tableData = [];
  //分页数据事件相关
  pages = {
    pageSize: 10,
    page: 1,
    totalSize: 120,
  };
  id = "";
  public isShow = false;
  get treeSbSsdsId() {
    return ParameterManagementModule.treeSbIdName.ssdsId;
  }
  get parentId() {
    return ParameterManagementModule.treeSbIdName.parentId;
  }
  get bdzId() {
    return ParameterManagementModule.treeSbIdName.bdzId;
  }
  get treeId() {
    return ParameterManagementModule.treeSbIdName.treeId;
  }
  get TreeId() {
    return ParameterManagementModule.TreeId;
  }
  // 树图最后一层设备名称
  get deName() {
    return ParameterManagementModule.treeSbIdName.name;
  }
  //测点类型
  get getPointTypeList() {
    // console.log(ParameterManagementModule.getPointTypeList);

    return ParameterManagementModule.getPointTypeList;
  }
  //系统名称
  get getSystemList() {
    return ParameterManagementModule.getSystemList;
  }
  get getAreaList() {
    return ParameterManagementModule.getAreaList;
  }
  @Watch("formData", { immediate: true, deep: true })
  public changeTop(to: any) {
    this.init();
  }
  @Watch("treeId", { immediate: true, deep: true })
  changeTree() {
    this.topDeviceName();
  }
  // 设备名称传入
  topDeviceName() {
    if (this.TreeId === "cd") {
      // 最后一层
      (this as any).formData.pointName = this.deName || "";
    } else {
      (this as any).formData.pointName = "";
    }
  }
  //表格数据
  handleSelectionChange(val: any) {
    this.multipleSelection = val;
    // console.log(this.multipleSelection);
    this.id = "";
    this.multipleSelection.map((item: any) => {
      this.id = this.id + "," + item.pointId;
    });
    this.id = this.id.substr(1);
  }
  //   分页
  handleSizeChange(val: any) {
    // console.log(`每页 ${val} 条`);
    this.pages.pageSize = val;
    this.init();
  }
  // 分页事件
  handleCurrentChange(val: any) {
    this.pages.page = val;
    this.init();
  }
  tableRowClassName({ row, rowIndex }: any): string {
    // console.log(row, rowIndex);
    if (rowIndex % 2 === 0) {
      return "row1";
    } else {
      return "row2";
    }
  }
  //删除
  cutOut() {
    // console.log("删除");
    if (this.multipleSelection.length > 0) {
      this.$confirm("是否删除", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          (this as any).$services
            .deleteFkPoint({ data: { ids: this.id }, method: "delete" })
            .then((result: any) => {
              //  console.log(result);
              if (result.code === 200) {
                this.$message({
                  showClose: true,
                  message: result.message,
                  type: "success",
                });
                this.init();
              } else {
                this.$message({
                  showClose: true,
                  message: result.message,
                  type: "warning",
                });
              }
            })
            .catch((err: object) => {
              //  console.log(err);
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消",
          });
        });
    } else {
      this.$message({
        showClose: true,
        message: "至少选择一条",
        type: "warning",
      });
    }
  }
  //关闭弹框
  handleClose() {
    this.dialogVisible = false;
  }
  determine() {
    this.dialogVisible = false;
  }
  // 新建
  submitForm() {
    this.PopUptitle = "新建";
    this.isShow = true;

    this.dialogVisible = true;
  }
  // 修改
  modify() {
    this.PopUptitle = "修改";
    if (this.multipleSelection.length != 1) {
      this.$message({
        type: "warning",
        message: "请勾选一条数据",
      });
    } else {
      this.isShow = true;
      //  console.log(this.multipleSelection);

      this.dialogVisible = true;
    }
  }
  // 查询
  init() {
    (this as any).formData.page = (this as any).pages.page;
    (this as any).formData.size = (this as any).pages.pageSize;
    this.formData.area = this.parentId;
    this.formData.station = this.bdzId;
    (this as any).$services
      .queryFkPoint({ data: this.formData, method: "post" })
      .then((result: any) => {
        // console.log(result);
        this.pages.totalSize = result.data.total;

        let tableData = result.data.records;
        let page = this.pages.page;
        let size = this.pages.pageSize;
        if (tableData && tableData.length) {
          tableData.forEach((v, i) => {
            // console.log(v)
            v.createTime = time(v.createTime) || "";
            v.updateTime = time(v.updateTime) || "";

            v.myIndex = (page - 1) * size + i + 1;
          });
        }
        this.tableData = tableData;

        // this.tableData=result.data.records
      })
      .catch((err: object) => {
        this.tableData = [];
        console.log(err);
      });
  }
  //是否展示文件 选择上传
  isShowFile = true;
  //模板导入
  mbgx() {
    document.getElementById("file0").click();
  }
  //模板导入
  fileUpload() {
    //  console.log(111);
    this.isShowFile = false;
    var files = (document.getElementById("file0") as any).files[0];
    var fd = new FormData();
    fd.append("excelFile", files);
    // var stationId=(this as any).tableData[0].stationId || ""
    // var stationName= (this as any).tableData[0].stationName || ""
    // var area= (this as any).tableData[0].area || ""
    // fd.append('bdzid',stationId);
    // fd.append('bdzmc',stationName);
    // fd.append('area',area);
    (this as any).$services
      .importPointExcel({
        data: fd,
        method: "post",
        processData: false,
        contentType: false,
        fromdata: true,
      })
      .then((result: any) => {
        this.$message({
          showClose: true,
          message: "导入成功",
          type: "success",
        });
        this.init();
        this.isShowFile = true;
      })
      .catch((err: any) => {
        // console.log(err);

        this.$message({
          showClose: true,
          message: err.msg,
          type: "warning",
        });
        this.isShowFile = true;
      });
  }
  // 模板下载
  templateDownload(): void {
    // console.log("模板下载");
    var a = document.createElement("a");
    a.download = name || "pic";
    a.href = "/tzglMb/cdwhmb.xlsx";
    a.click();
  }
  mounted() {
    this.formData.area = this.parentId;
    this.init();
    this.formData.station = this.bdzId;
    (this as any).$services
      .getStationList({ data: { ssds: this.treeSbSsdsId } })
      .then((result: any) => {
        this.stationList = result.data;
        // console.log(this.stationList);
      })
      .catch((err: object) => {
        console.log(err);
      });
  }
}
</script>

<style lang="scss" scoped>
.RightFzjkDetails {
  width: 100%;
  height: 100%;
  .elForm-box {
    padding: 0.24rem 0.2rem 0 0;
  }
  .inspection-report-index-btn {
    display: flex;
    padding-left: 0.46rem;
    padding-top: 0.02rem;
    .el-button + .el-button {
      margin-left: 0.1rem;
    }
  }
  .tableed {
    width: 100%;
    height: calc(100% - 2rem);
  }
  .paged {
    width: 100%;
    height: 0.6rem;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  #file0 {
    display: none;
  }
}
</style>
<style lang="scss">
.RightFzjkDetails {
  .inspection-report-index-btn {
    .el-button {
      padding: 0.12rem 0.2rem;
    }
  }
  .el-form-item {
    margin-bottom: 0.22rem;
  }
  .el-form-item--medium .el-form-item__content,
  .el-form-item--medium .el-form-item__label {
    line-height: 0.36rem;
  }

  // .el-dialog {
  //   // height: 5.5rem;
  // }
  .el-dialog__body {
    height: calc(100% - 44px);
  }
  //导入
  .el-upload-list {
    display: none;
  }
  .el-upload {
    margin: 0 0.1rem;
  }
}
</style>
